import { Injectable } from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {FormUtil} from "../../../util/form-util";
import {Observable} from "rxjs";
import {MsgResult} from "../../../models/msg-result";
import {MetaTemplate} from "./meta-template";
import {TemplateAPI} from "../template-constants";

@Injectable()
export class MetaTemplateService {

  constructor(
    private request: RequestUtil,
    private formUtil: FormUtil
  ) { }

  //添加实例
  addMetaTemplate(metaTemplate: MetaTemplate): Observable<MsgResult> {
    let params = "name="+metaTemplate.name;
    params += "&type="+metaTemplate.type;
    params += "&orderBy="+metaTemplate.orderBy;
    params += "&description="+metaTemplate.description;
    params += "&content="+ encodeURI(encodeURI(metaTemplate.content));
    params += "&isUse="+metaTemplate.isUse;
    return this.request.post(TemplateAPI.insertMetaTemplate, params);
  }

  //删除实例
  deleteMetaTemplate(id: string): Observable<MsgResult> {
    return this.request.delete(TemplateAPI.deleteMetaTemplate + "&id=" + id);
  }

  //批量删除
  deleteMetaTemplates(ids: Array<String>): Observable<MsgResult> {
    let params = "idStrs=" + ids;
    return this.request.post(TemplateAPI.delMetaTemplates, params);
  }

  //根据id获得规则
  getMetaTemplateById(id: string): Observable<MsgResult> {
    return this.request.get(TemplateAPI.getMetaTemplateById + "&id=" + id);
  }

  //修改实例
  updateMetaTemplate(metaTemplate: MetaTemplate): Observable<MsgResult> {
    let params = "name="+metaTemplate.name;
    params += "&id="+metaTemplate.id;
    params += "&type="+metaTemplate.type;
    params += "&orderBy="+metaTemplate.orderBy;
    params += "&description="+metaTemplate.description;
    params += "&content="+ encodeURI(encodeURI(metaTemplate.content));
    params += "&isUse="+metaTemplate.isUse;
    return this.request.put(TemplateAPI.updateMetaTemplate, params);
  }

}
